package com.zat.mes.andrmes.dofun.work.vm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.OutSourceBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response

/**
 * 外协入库
 */
class OutSourceModel : ViewModel() {

    private val outSourceData: MutableLiveData<OutSourceBean> = MutableLiveData()
    private val postData: MutableLiveData<String> = MutableLiveData()


    fun getOutsourceList(): LiveData<OutSourceBean> {
        return outSourceData
    }

    fun getOutsourcePostData(): LiveData<String> {
        return postData
    }

    /**
     * 获取 外协数据
     */
    fun loadInhouseList(type: Int, code: String?) {

        RetrofitManager.apiService(ApiService::class.java)
            .getOutSourceList(type, code)
            .enqueue(object : MesCallBack<BaseResponse<OutSourceBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<OutSourceBean>>?,
                    response: Response<BaseResponse<OutSourceBean>>?
                ) {
                    outSourceData.postValue(response?.body()?.result)
                }


            })
    }

    /**
     * bom 入库
     */
    fun loadInhouseBom(jsonArray: String) {

        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, jsonArray)
        RetrofitManager.apiService(ApiService::class.java)
            .postBomInhouse(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postData.postValue(response?.body()?.message)
                }


            })
    }

    /**
     * pack 入库
     */
    fun loadInhousePack(jsonArray: String) {
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, jsonArray)
        RetrofitManager.apiService(ApiService::class.java)
            .postPackInhouse(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postData.postValue(response?.body()?.message)
                }


            })
    }

    /**
     * 成品 入库
     */
    fun loadInhouseProduct(jsonArray: String) {
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, jsonArray)
        RetrofitManager.apiService(ApiService::class.java)
            .postProductInhouse(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postData.postValue(response?.body()?.message)
                }


            })
    }


}
